package io.chenglicun.modules.data.service.statistics.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.modules.data.dao.statistics.DataCategoryStatisticsDao;
import io.chenglicun.modules.data.dto.statistics.DataCategoryStatisticsDTO;
import io.chenglicun.modules.data.entity.statistics.DataCategoryStatisticsEntity;
import io.chenglicun.modules.data.service.statistics.DataCategoryStatisticsService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类型统计数据
 *
 * @author pipi 1151485299@qq.com
 * @since 1.0 2024-03-02
 */
@Service
public class DataCategoryStatisticsServiceImpl extends CrudServiceImpl<DataCategoryStatisticsDao, DataCategoryStatisticsEntity, DataCategoryStatisticsDTO> implements DataCategoryStatisticsService {

    @Override
    public QueryWrapper<DataCategoryStatisticsEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<DataCategoryStatisticsEntity> wrapper = new QueryWrapper<>();
        Object type = params.get("type");
        Object category = params.get("category");
        wrapper.eq(ObjectUtil.isNotEmpty(type), "type", type);
        wrapper.eq(ObjectUtil.isNotEmpty(category), "category", category);
        return wrapper;
    }




    @Override
    public List<DataCategoryStatisticsDTO> selectByType(String type) {
        Map map = new HashMap<>();
        map.put("type", type);
        return baseDao.selectList(getWrapper(map));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DataCategoryStatisticsDTO dto){
        Map map = new HashMap<>();
        map.put("type", dto.getType());
        map.put("category",dto.getCategory());
        DataCategoryStatisticsEntity existEntity = baseDao.selectOne(getWrapper(map));
        if (existEntity != null){
            baseDao.deleteById(existEntity.getId());
        }
        super.save(dto);
    }
}